package cloud.xlh.rabbitmq;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/10/16 14:27
 * @ Description:
 */
@Component
public class OrderConsumer {
    /**
     * 监听延迟订单队列
     */
    /*@RabbitListener(queues = {"order.delay.expire.queue"})
    public void handleDelayQueue(Message message, Channel channel) throws IOException {
        System.out.println("延迟订单消费者类，拿到消息：" + new String(message.getBody()));
        try {
            //完毕，一定要记得手动ACK
            //channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e){
            //发生异常，进行NACK
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }*/

    /**
     * 监听死信队列
     * 进入死信队列的条件：
     * 1、队列达到最大长度
     * 2、消息被拒
     * 3、消息 TTL 过期
     * 4、客户端和服务器端断开连接
     */
    @RabbitListener(queues = {"dead.letter.queue"})
    public void handleDeadQueue(Message message, Channel channel) throws IOException {
        System.out.println("拿到死信消息：" + new String(message.getBody()));

        /**
         * 这里取到消息后，根据实际业务规则，进行处理即可
         */
        //todo 订单状态改为已过期

        //最后手动ACK
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
